package com.tlx.app.dedao;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

import com.tlx.core.util.TFileUtils;
import com.tlx.core.util.TSimpleThreadPool;
import com.tlx.image.ImageJoinUtils;
import com.tlx.image.ImageQualityUtils;

public class GroupBean {
    static {
        TSimpleThreadPool.init(5);
    }

    protected String gid;
    protected String gname;

    protected String srcDir;
    protected List<String> srcImgList = new ArrayList<String>();
    protected String srcAudio;
    protected List<String> srcPdfList = new ArrayList<String>();

    protected String lastDir;
    protected String lastImg;
    protected String lastPdf;
    protected String lastAudio;

    public GroupBean() {
    }

    public GroupBean(String gi, String gn, String srcDir, String lastDir) {
        this.gid = gi;
        this.gname = gn;
        this.lastDir = lastDir;
        this.srcDir = srcDir;

        // ****************************初始化图片【开始】****************************
        List<String> imgList = TFileUtils.getImageList(this.srcDir);
        for (String img : imgList) {
            if (FilenameUtils.getBaseName(img).startsWith(gid)) {
                srcImgList.add(img);
            }
        }
        this.lastImg = new File(this.lastDir, gid + gname + ".jpg").getAbsolutePath();
        if (CollectionUtils.isNotEmpty(srcImgList) && new File(lastImg).exists()) {
            throw new RuntimeException("目标文件存在：" + "【" + lastImg + "】");
        }
        // ****************************初始化图片【结束】****************************

        initAudio();

        initPdf();

        // 创建最终目录
        if (!new File(lastDir).exists()) {
            new File(lastDir).mkdirs();
        }
    }

    private void initAudio() {
        List<String> audioList = TFileUtils.getFileListByExt(this.srcDir, "mp3", "m4a");
        for (String audio : audioList) {
            if (FilenameUtils.getBaseName(audio).startsWith(gid)) {
                if (StringUtils.isNotBlank(lastAudio)) {
                    // 有值表示重复了
                    this.srcAudio = null;
                    this.lastAudio = null;
                }
                this.srcAudio = audio;
                String ext = FilenameUtils.getExtension(this.srcAudio);
                this.lastAudio = new File(this.lastDir, gid + gname + "." + ext).getAbsolutePath();
            }
        }
        audioList = audioList.size() > 1 ? new ArrayList<String>() : audioList;// 超过1个文件属于异常，先不处理，人工处理
        if (StringUtils.isNotBlank(this.srcAudio) && new File(lastAudio).exists()) {
            throw new RuntimeException("目标文件存在：" + "【" + lastAudio + "】");
        }
    }

    protected void initPdf() {

    }

    public void genAll() {
        TSimpleThreadPool.submit(new Runnable() {

            @Override
            public void run() {
                if (CollectionUtils.isNotEmpty(srcImgList) && StringUtils.isNotBlank(srcAudio)) {
                    System.out.println("【开始】" + gid + ":" + gname);
                    GroupBean.this.genImg();
                    GroupBean.this.genMp3();
                    GroupBean.this.genPdf();
                    System.out.println("---------------------" + gid + ":" + gname + "【完成】");
                }
            }
        });

    }

    public void genImg() {
        if (CollectionUtils.isNotEmpty(srcImgList)) {
            ImageJoinUtils.joinVertical(srcImgList, lastImg);
            ImageQualityUtils.qualitySelf(lastImg, 75);
            TFileUtils.deleteListQuietly(srcImgList);
        }
    }

    public void genMp3() {
        if (StringUtils.isNotBlank(srcAudio)) {
            TFileUtils.rename(srcAudio, lastAudio);
            TFileUtils.deleteQuietly(srcAudio);
        }
    }

    public void genPdf() {

    }

}
